package org.jeecg.modules.controller.print;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.bean.entity.print.TbPrintChargeRecord;
import org.jeecg.modules.bean.vo.print.ChargeCountsVo;
import org.jeecg.modules.bean.vo.print.PrintChargeRecordVo;
import org.jeecg.modules.service.print.ITbPrintChargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 打印收费记录表
 * @Author: jeecg-boot
 * @Date: 2023-10-19
 * @Version: V1.0
 */
@Api(tags = "打印收费记录表")
@RestController
@RequestMapping("/print/tbPrintChargeRecord")
@Slf4j
public class TbPrintChargeRecordController extends JeecgController<TbPrintChargeRecord, ITbPrintChargeRecordService> {

    private static final String ERRO_CORRESPONDING = "未找到对应数据";


    @Autowired
    private ITbPrintChargeRecordService tbPrintChargeRecordService;


    /**
     * 分页列表查询
     *
     * @param tbPrintChargeRecord
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "打印收费记录表-分页列表查询", notes = "打印收费记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<TbPrintChargeRecord>> queryPageList(TbPrintChargeRecord tbPrintChargeRecord,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            @RequestParam(name = "operatorIDS",required = false) String[] operatorIDS,
                                                            HttpServletRequest req) {
        QueryWrapper<TbPrintChargeRecord> queryWrapper = QueryGenerator.initQueryWrapper(tbPrintChargeRecord, req.getParameterMap());
        if (operatorIDS != null && operatorIDS.length > 0) {
            queryWrapper.in("operator_id", operatorIDS);
        }
        Page<TbPrintChargeRecord> page = new Page<>(pageNo, pageSize);
        IPage<TbPrintChargeRecord> pageList = tbPrintChargeRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param tbPrintChargeRecord
     * @return
     */
    @AutoLog(value = "打印收费记录表-添加")
    @ApiOperation(value = "打印收费记录表-添加", notes = "打印收费记录表-添加")
//	@RequiresPermissions("print:tb_print_charge_record:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody TbPrintChargeRecord tbPrintChargeRecord) {
        if (null == tbPrintChargeRecord || null == tbPrintChargeRecord.getUserId()) {
            return Result.error(500, "参数为空，请确认！！");
        }
        QueryWrapper<TbPrintChargeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", tbPrintChargeRecord.getUserId());
        queryWrapper.eq("payment_state", tbPrintChargeRecord.getPaymentState());
        TbPrintChargeRecord printChargeRecord = tbPrintChargeRecordService.getOne(queryWrapper);
        if (null == printChargeRecord) {
            tbPrintChargeRecordService.save(tbPrintChargeRecord);
            return Result.OK("添加成功！");
        } else {
            return Result.error("此费用信息已存在，不可重复添加");
        }
    }

    /**
     * 编辑
     *
     * @param tbPrintChargeRecord
     * @return
     */
    @AutoLog(value = "打印收费记录表-编辑")
    @ApiOperation(value = "打印收费记录表-编辑", notes = "打印收费记录表-编辑")
//    @RequiresPermissions("print:tb_print_charge_record:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody TbPrintChargeRecord tbPrintChargeRecord) {
        tbPrintChargeRecordService.updateById(tbPrintChargeRecord);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "打印收费记录表-通过id删除")
    @ApiOperation(value = "打印收费记录表-通过id删除", notes = "打印收费记录表-通过id删除")
    @RequiresPermissions("print:tb_print_charge_record:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        tbPrintChargeRecordService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "打印收费记录表-批量删除")
    @ApiOperation(value = "打印收费记录表-批量删除", notes = "打印收费记录表-批量删除")
    @RequiresPermissions("print:tb_print_charge_record:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.tbPrintChargeRecordService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "打印收费记录表-通过id查询")
    @ApiOperation(value = "打印收费记录表-通过id查询", notes = "打印收费记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<TbPrintChargeRecord> queryById(@RequestParam(name = "id", required = true) String id) {
        TbPrintChargeRecord tbPrintChargeRecord = tbPrintChargeRecordService.getById(id);
        if (tbPrintChargeRecord == null) {
            return Result.error(ERRO_CORRESPONDING);
        }
        return Result.OK(tbPrintChargeRecord);
    }

    @ApiOperation(value = "查询收费记录-通过患者id和支付状态", notes = "查询收费记录-通过患者id和支付状态")
    @GetMapping(value = "/queryFeeListByUserIdAndStatus")
    public Result<TbPrintChargeRecord> queryFeeListByUserIdAndStatus(TbPrintChargeRecord tbPrintChargeRecord) {
        QueryWrapper<TbPrintChargeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", tbPrintChargeRecord.getUserId());
        queryWrapper.eq("payment_state", tbPrintChargeRecord.getPaymentState());
        TbPrintChargeRecord feeRecord = tbPrintChargeRecordService.getOne(queryWrapper);
        if (feeRecord == null) {
            return Result.error(ERRO_CORRESPONDING);
        }
        return Result.OK(feeRecord);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param tbPrintChargeRecord
     */
    @RequiresPermissions("print:tb_print_charge_record:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbPrintChargeRecord tbPrintChargeRecord) {
        return super.exportXls(request, tbPrintChargeRecord, TbPrintChargeRecord.class, "打印收费记录表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("print:tb_print_charge_record:importExcel")
    @PostMapping(value = "/importExcel")
    public Result<String> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TbPrintChargeRecord.class);
    }

    @ApiOperation(value = "收费统计", notes = "收费统计")
    @GetMapping(value = "/queryByDateType")
    public Result<PrintChargeRecordVo> queryByDateType(@RequestParam(name = "dateType", required = true) Integer dateType,
                                                       @RequestParam(name = "startTime", required = true) String startTime, String endTime) {
        Integer[] typeArr = {1, 2, 3};
        if (Arrays.binarySearch(typeArr, dateType) < 0) {
            return Result.error("时间类型错误");
        }
        PrintChargeRecordVo printChargeRecordVo = tbPrintChargeRecordService.queryByDateType(dateType, startTime, endTime);
        if (printChargeRecordVo == null) {
            return Result.error(ERRO_CORRESPONDING);
        }

        return Result.OK(printChargeRecordVo);
    }

    @ApiOperation(value = "费用结算统计", notes = "费用结算统计")
    @GetMapping(value = "/queryByDateType1")
    public Result<List<PrintChargeRecordVo>> queryCountsByItems(String startTime, String endTime,
                                                          TbPrintChargeRecord chargeRecord,String[] operatorIDS) {
        List<PrintChargeRecordVo>  printChargeRecordVo = tbPrintChargeRecordService.queryFeeCounts(startTime, endTime,chargeRecord,operatorIDS);
        if (printChargeRecordVo.isEmpty()) {
            return Result.error(ERRO_CORRESPONDING);
        }
        return Result.OK(printChargeRecordVo);
    }

    @ApiOperation(value = "费用金额统计", notes = "费用金额统计")
    @GetMapping(value = "/queryCounts")
    public Result<ChargeCountsVo> queryCounts(String startTime, String endTime,
                                              TbPrintChargeRecord chargeRecord,String... operatorIDS) {
        ChargeCountsVo  countsVo = tbPrintChargeRecordService.queryCounts(startTime, endTime,chargeRecord,operatorIDS);
        System.out.println("countsVo = " + countsVo);
        if (countsVo == null) {
            return Result.error(ERRO_CORRESPONDING);
        }
        return Result.OK(countsVo);
    }
}
